package pers.cz.easyexcel.dao;

import pers.cz.common.connect.ConnectionFactory;
import pers.cz.common.connect.IConnection;
import pers.cz.common.connect.StoreType;
import pers.cz.common.connect.localfile.LocalFileConnection;
import pers.cz.common.exception.ErrorCode;
import pers.cz.common.exception.OrmException;
import pers.cz.common.pub.AbstractEntity;
import pers.cz.common.pub.CommonDao;
import pers.cz.easyexcel.localfile.LocalFileConnectionFactory;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @program: Jef-orm
 * @description: 本地文件类dao层实现
 * @author: Cheng Zhi
 * @create: 2023-09-04 20:05
 **/
public class CommonLocalFileDaoImpl extends AbstractFileDao{

    public CommonLocalFileDaoImpl(File file) {
        super(file);
    }

    @Override
    public List<? extends AbstractEntity> find(AbstractEntity abstractEntity) {

        try {
            return analysisFile(abstractEntity);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List find(AbstractEntity abstractEntity, DataSource dataSource) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }

    @Override
    public int insert(AbstractEntity abstractEntity) {

        try {
            writeFile(abstractEntity, false);
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int insert(AbstractEntity abstractEntity, DataSource dataSource) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }

    @Override
    public int override(AbstractEntity abstractEntity) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }

    @Override
    public int override(AbstractEntity abstractEntity, DataSource dataSource) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }

    @Override
    public int batchInsert(List abstractEntitys) {

        try {
            writeFile(abstractEntitys);
            return abstractEntitys.size();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int batchInsert(List abstractEntitys, DataSource dataSource) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }

    @Override
    public int update(AbstractEntity abstractEntity) {
        try {
            writeFile(abstractEntity, true);
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(AbstractEntity abstractEntity, DataSource dataSource) {
        return 0;
    }

    @Override
    public int delete(AbstractEntity abstractEntity) {
        try {
            remove(abstractEntity);
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(AbstractEntity abstractEntity, DataSource dataSource) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }

    @Override
    public List selectAs(String sql, Class resultType, Map map, DataSource dataSource) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }

    @Override
    public List selectAs(String sql, Class resultType, DataSource dataSource) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }

    @Override
    public List selectAs(String sql, Class resultType) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }


    @Override
    public List selectAs(String sql, Class resultType, Map map) {
        throw new OrmException(ErrorCode.ORM_EXCEL_NOT_SUPPORTED_ERROR);
    }
}
